//
//  MsgCell.m
//  WeiMo
//
//  Created by Lixiaoyong on 13-3-22.
//  Copyright (c) 2013年 . All rights reserved.
//

#import "MsgCell.h"

#import "UIImageView+WebCache.h"


@implementation MsgCell
@synthesize headImage,headIdentify;
@synthesize title,content,time;
@synthesize sender;
@synthesize headImageUrl;
@synthesize delegate;
@synthesize inboxSenderId;
@synthesize userType;

- (void)dealloc
{
    self.headImage = nil;
    self.headIdentify = nil;
    self.title = nil;
    self.time = nil;
    self.content = nil;
    self.sender = nil;
    self.headImageUrl = nil;
    self.inboxSenderId = nil;
    self.userType = nil;
    [super dealloc];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        CGRect rect = CGRectMake(5, 16, 42, 42);
        UIColor *color = [UIColor blackColor];
        UIColor *bgColor = [UIColor clearColor];
        UIFont *font = [UIFont fontWithName:@"Helvetia-Bold" size:11.5];
        
        self.headImage = [[[TouchImageView alloc] initWithFrame:rect] autorelease];
        self.headImage.delegate = self;
        [self addSubview:self.headImage];
        
        self.title = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
        self.title.textColor = color;
        self.title.font = font;
        self.title.backgroundColor = bgColor;
        [self addSubview:self.title];
        
        self.content = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
        self.content.textColor = color;
        self.content.font = [UIFont systemFontOfSize:15.0];
        self.content.numberOfLines = 0;
        self.content.lineBreakMode = NSLineBreakByTruncatingTail;
        self.content.backgroundColor = bgColor;
        [self addSubview:self.content];
        
        self.time = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
        self.time.textColor = [UIColor blackColor];
        self.time.font = [UIFont systemFontOfSize:11.5];
        self.time.backgroundColor = bgColor;
        [self addSubview:self.time];

        self.sender =[[[UIButton alloc] initWithFrame:CGRectZero] autorelease];
        [self.sender addTarget:self action:@selector(senderPrivateMessage:) forControlEvents:UIControlEventTouchUpInside];
        [self.sender setTitle:@"发送个人资料" forState:UIControlStateNormal];
        self.sender.titleLabel.font = [UIFont systemFontOfSize:13.0];
        self.sender.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
        self.sender.titleLabel.textAlignment = UITextAlignmentCenter;
        [self.sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.sender setBackgroundImage:[UIImage imageNamed:@"sendMyInfo.png"] forState:UIControlStateNormal];
        [self addSubview:self.sender];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

- (void)refreshRect
{
    
    [self.headImage setImageWithURL:[NSURL URLWithString:self.headImageUrl]
              placeholderImage:[UIImage imageNamed:@"touxiang.png"]];
    
    CGSize size = [self returnRectWithString:self.title.text];
    size.height = 18;
    CGRect rect = CGRectMake(60-10, 16, 320 - 60, size.height);
    self.title.frame = rect;
    
    size = [self returnRectWithString:self.content.text];
    rect.origin.y = rect.origin.y + rect.size.height + 4;
    rect.size = size;
    rect.size.height = rect.size.height;
    rect.origin.x = 60;
    self.content.frame = rect;
    
    rect.origin.y = rect.origin.y + rect.size.height + 8;
    rect.size = CGSizeMake(120, 30);
    self.time.frame = rect;
    
    rect.origin.x = rect.origin.x + rect.size.width + 30;
    rect.size.height = 29;
    rect.size.width  = 110;
    self.sender.frame = rect;
    
    //[self setNeedsDisplay];
}

- (CGSize)returnRectWithString:(NSString *)string
{
    UIFont *font = [UIFont systemFontOfSize:15.0];
    CGSize size = [string sizeWithFont:font constrainedToSize:CGSizeMake(256, 1000) lineBreakMode:NSLineBreakByTruncatingTail];
    return size;
}

- (void)senderPrivateMessage:(UIButton *)button
{
    if (self.delegate)
    {
        int tag = button.tag;
        [self.delegate senderMessageClicked:tag];
    }
}

#pragma mark - TouchImageViewDelegate

- (void)clickImageView:(TouchImageView *)imageView
{
    if (self.delegate)
    {
        [self.delegate clickedInboxSender:self.inboxSenderId userType:self.userType];
    }
}

@end